package com.midea.iot.course1.javaSE4;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 作业2
 * @author xingwuxw
 * 题目：编写程序，循环接收用户从键盘输入多个字符串，直到输入“end”时循环结束，
 * 并将所有已输入的字符串按字典顺序倒序录入到项目下abc.txt中
 * 参考答案
 */
public class LongStringScanner {

    public static void main(String[] args) {

        TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int num = s1.compareTo(s2);
                return num == 0 ? 1 :num;
            }
        });

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串！");
        while(true){
            String line = sc.nextLine();
            if("end".equals(line)){
                System.out.println("输入完毕！");
                break;
            }
            ts.add(line);
        }
        String filePath = "abc.txt";
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(ts);// 往文件里写入字符串
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
